#_*_ coding:utf-8 _* _

#io操作类
import os
import shutil
from typing import Union

class FileUtil():
	#写文件
	def write_file(self,content: str, file_name: Union[str, bytes, os.PathLike]) -> None:
		content = content.encode('utf-8')
		# 获取文件夹路径
		dir_name = os.path.dirname(file_name)

		# 如果文件夹不存在，则创建它
		if not os.path.exists(dir_name):
			os.makedirs(dir_name)

		# 写入文件
		with open(file_name, 'wb') as f:
			f.write(content)

	#读取文件
	def read_text_file(self,file_name: str) -> str:
		with open(file_name, 'r') as f:
			return f.read()

	#分行读取文件
	def read_text_file_as_list(self,file_name: str) -> list:
		with open(file_name, 'r') as f:
			return f.readlines()

	#删除文件
	def delete_path(self,path: Union[str, bytes, os.PathLike]) -> None:
		if os.path.isfile(path):
			# 删除文件
			os.remove(path)
		elif os.path.isdir(path):
			# 删除文件夹
			shutil.rmtree(path)
		